package com.yiduan.apollocrm.views.mediators
{
	
	import com.pialabs.eskimo.events.ContextableListEvent;
	import com.yiduan.apollocrm.models.Lead;
	import com.yiduan.apollocrm.views.LeadsView;
	import com.yiduan.apollocrm.views.NewLeadView;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.collections.ArrayCollection;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Label;
	import spark.events.TextOperationEvent;
	
	public class LeadsMediator extends Mediator
	{
		[Inject]
		public var leadsView:LeadsView;
		
		private var filterText:String="";
		
		public function LeadsMediator() 
		{
			super();
		}
		
		override public function onRegister():void
		{
			initLeadDataProvider();
			leadsView.newLeadBtn.addEventListener(MouseEvent.CLICK, newLeadHandler);	
			leadsView.filterLeadTextInput.addEventListener(TextOperationEvent.CHANGE,changeFilter);
			leadsView.leadList.addEventListener(ContextableListEvent.CONTEX_MENU_ITEM_CLICK,leadListContextMenuHandler);
		}
		
		protected function newLeadHandler(e:Event):void
		{
			leadsView.navigator.pushView(NewLeadView);
		}
		
		protected function initLeadDataProvider():void
		{	
			this.leadsView.leadListDataProvider = fakeTwoLeads();
			this.leadsView.leadListDataProvider.filterFunction = filterLeads;			
		}
		
		protected function changeFilter(event:TextOperationEvent):void
		{
			filterText = this.leadsView.filterLeadTextInput.text;
			this.leadsView.leadListDataProvider.refresh();
		}
		
		private function filterLeads(o:Object):Boolean
		{
			var lead:Lead = o as Lead;
			if (lead!=null)
			{
				if (lead.fullName.toUpperCase().indexOf(filterText) != -1)
				{
					return true;
				}
				return false;
			}
			return false;
		}
		
		private function fakeTwoLeads():ArrayCollection
		{
			var arrayCollection:ArrayCollection = new ArrayCollection();
			var lead1:Lead = new Lead();
			lead1.id =1;
			lead1.fullName = "张三";
			lead1.firstName = "三";
			lead1.lastName = "张";
			lead1.email = "zhangsan@gmail.com";
			lead1.phoneNumber = "13112366549";
			lead1.company  = "肯德基";
			
			var lead2:Lead = new Lead();
			lead2.id = 2;
			lead2.fullName = "李四";
			lead2.firstName = "四";
			lead2.lastName = "李";
			lead2.email = "lisi@gmail.com";
			lead2.phoneNumber = "13890786543";
			lead2.company = "铁道部";
			
			var lead3:Lead = new Lead();
			lead3.id = 3;
			lead3.fullName = "王五";
			lead3.firstName = "五";
			lead3.lastName = "王";
			lead3.email = "wangwu@gmail.com";
			lead3.phoneNumber = "13890786543";
			lead3.company = "电信";
			
			var lead4:Lead = new Lead();
			lead4.id = 4;
			lead4.fullName = "赵六";
			lead4.firstName = "六";
			lead4.lastName = "赵";
			lead4.email = "lisi@gmail.com";
			lead4.phoneNumber = "13890786543";
			lead4.company = "铁道部";
			
			arrayCollection.addItem(lead1);
			arrayCollection.addItem(lead2);
			arrayCollection.addItem(lead3);
			arrayCollection.addItem(lead4);
			
			return arrayCollection;
		}
		
		protected function leadListContextMenuHandler(event:ContextableListEvent):void
		{
			var lead:Lead = event.item as Lead;	
			
			if (event.menuItem.name == "editContextMenu")
			{
				goToNewLeadView(lead);
			}
			else if (event.menuItem.name == "callContextMenu")
			{
				callLead(lead);
			}
			else if (event.menuItem.name == "messageContextMenu")
			{
				sentMessage(lead);
			}
				
			else if (event.menuItem.name == "mailContextMenu") 
			{
				mailLead(lead);
			}
			else if (event.menuItem.name == "deleteContextMenu")
			{
				deleteLead(event.itemIndex);
			}
		}
		
		private function goToNewLeadView(lead:Lead):void
		{			
			this.leadsView.navigator.pushView(NewLeadView,lead);
		}
		
		private function callLead(lead:Lead):void
		{
			if (lead!=null)
			{
				var callString:String = "";
				callString+= "tel:";
				callString+=lead.phoneNumber;
				navigateToURL(new URLRequest(callString));
			}
		}
		
		private function mailLead(lead:Lead):void
		{
			if (lead!=null)
			{
				var mailString:String = "";
				mailString+="mailto:";
				mailString+=lead.email;
				navigateToURL(new URLRequest(mailString));
			}
		}
		
		private function sentMessage(lead:Lead):void
		{
			if (lead!=null)
			{
				var textString:String = "";
				textString+="sms:";
				textString+=lead.phoneNumber;
				navigateToURL(new URLRequest(textString));
			}
		}
		
		private function deleteLead(index:int):void
		{
			leadsView.leadList.dataProvider.removeItemAt(index);
		}

	}
}